package com.day1;

import javax.servlet.ServletException;

import javax.servlet.http.*;
import java.io.IOException;



import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class User extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.service(req, resp);
        req.setCharacterEncoding("utf-8");


        resp.setContentType("text/html;charset=utf-8");
        String name = req.getParameter("username");
        String psw = req.getParameter("psw");
        Map<String,String> list=new HashMap<>();
        list.put("a1","111");
        list.put("a2","222");

        HttpSession session=req.getSession();
        session.setAttribute("session",list);

        Set<String> set=list.keySet();
        boolean f=false;
        for (String s:set){
            if(name.equals(s)&&psw.equals(list.get(s))){
                f=true;

            }
        }
        if(f){
//            PrintWriter writer=resp.getWriter();
//            writer.print("<h2>你好"+name+"</h2>");
              req.setAttribute("name",name);
              Cookie ck1=new Cookie("name",name);
              ck1.setMaxAge(3600*24*3);
              resp.addCookie(ck1);
            req.getRequestDispatcher("success").forward(req,resp);
        }else{
//            resp.getWriter().print("用户名：<input type=\"text\" name=\"username\" />\n"+"<br />" +
//                    "        密码：<input type=\"password\" name=\"psw\"/><br />\n" +
//                    "        <input type=\"submit\" />");
            req.setAttribute("name",name);
            resp.sendRedirect("/hhh/fail");


        }
    }
}
